By _michel_ on
Hi.
How can I include a .js and a .css files in just one page; not the complete site.
Thanks in advance.
Michel.
Hi.
How can I include a .js and a .css files in just one page; not the complete site.
Thanks in advance.
Michel.
Comments
--
For the .js, you paste a drupal_add_js call somewhere in the content of the page. Can be in a block. I have tried this and it works : I put that call in a node content, because that particular node needed that .js file, and of course the input format was set to php.
For the .css you paste a drupal_add_css call somewhere in the content of the page. Can be in a block too I think. I never tried it. I should work theoretically. Don't know if that'll work if you preprocess your CSS files (you can set that on your admin 'performance' page).
I guess you could always modify your page.tpl.php template, and, using php, conveniently check drupal's internal path and link in the css file if that's the path thank you very much.
Caroline
Who am I | Where are we
11 heavens
It works perfectly..thanks a lot.
Hi,
It works fine :
drupal_add_css ('myfile.css');
drupal_add_js('myfile.js');
Thanks again. Merci.
Michel.
adding java for contact captcha script
Thanks everyone for helping me solve a similar problem. I too was trying to add a java script to only one page. I first added it to the page template file but the script caused conflicts with the page editor and it's expandable menus. I then simply added it to the page on which it was needed and it worked fine!
I was installing a third party 'contact us' captcha script and your comments helped to get it working.
Cheers,
Kent
http://www.clickbankaccountpro.com
i also want to inclide js
i also want to inclide js file in my template ...how can i incude that
i tried this
drupal_add_js('myfile.js');but not working
my js file is in template folder.
please help
reply
working fine for me,
just put like
drupal_add_css("module/custommodulename/file.css");
see view source
Be careful of hardcoding your
Be careful of hardcoding your module paths.
The drupal_add_css (and the js one too) functions take paths relative the the base path (base_path()). Modules can be in various locations, like sites/all/modules/{module} or sites/{mysite}/modules/{module}. The drupal_get_path() function should be used to find the path to an enabled theme or module.
Try this instead:
This will handle global , custom, and core modules|themes, and will also still work if a module is moved, (like taken from 3rd party to custom state, when moved from sites/all to sites/{mysite}.)
For DRP6 and on: Note also that including css files for themes is easier to do by including a reference to the css in the theme.info file. theme.info declarations are preferred unless the css file is not often included and large.
I'm still confused about how
I'm still confused about how to use javascript and css for just ONE page. I still can't figure out how to make a module for just ONE page - can it really be true that I need to do this and if yes can someone help me please?
what is a page
This is a late reply, so I guess that it doesn't work for you, but in case others see it and feel the same way:
The problem is that there is not a single source for what 'one page' is, so there are different ways to repond depending on what created the page:
It is generally considered a better approach to target elements on you page with a more precise selector, than to modify any core code.
JavaScript on specific page
You can just use something like this in template.php:
function mytheme_preprocess_page(&$vars, $hook) {
if('specific_pathname' === arg(0)) {
$jspath = path_to_theme().'/js/myjavascript.js';
drupal_add_js($jspath);
}
This might be
This might be simpler:
drupal_add_css( (path_to_theme() . /{mycssfile}.css'), 'theme', 'all');
I ended up doing it like
I ended up doing it like explained here http://groups.drupal.org/node/79804 making a googlemap.module and people can just copy and paste the code for their own purpose. Which I could make it a "real" module but still need to understand a lot and no one have offerede to help me further. :-(
where?
Could someone tell me exactly on which file or where I need to add a line of code like this to include a css file?
drupal_add_css( (path_to_theme() . /{mycssfile}.css'), 'theme', 'all');
Where does this go? I need it to be included on a newsletter email template. I tried putting it on the template itself but it doesn't work.